package tools;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.opera.OperaDriver;
import java.io.File;
import java.io.IOException;
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Date;
import static org.testng.Assert.assertEquals;
public class UseBrowser {
    public WebDriver driver;
    //启动谷歌浏览器
    public WebDriver setupChrome() throws IOException {
        String driverPath = new File("./").getCanonicalPath() + "/src/test/java/driver/chromedriver.exe";
        String driverName = "webdriver.chrome.driver";
        System.setProperty(driverName, driverPath);

        ChromeOptions chromeOptions = new ChromeOptions();


//        解决“chrome正受到自动测试软件的控制”信息栏显示
        chromeOptions.setExperimentalOption("excludeSwitches", new String[]{"enable-automation"});

        // System.setProperty("webdriver.chrome.driver", "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe");
        driver = new ChromeDriver(chromeOptions);
        //driver.get(test_url);
        driver.manage().window().maximize();
        // driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        return driver;
    }
    //启动Chrome-headless
    public WebDriver setupChromeHeadless() throws IOException {
        String driverPath = new File("./").getCanonicalPath() + "/src/test/java/driver/chromedriver.exe";
        String driverName = "webdriver.chrome.driver";
        System.setProperty(driverName, driverPath);
        // DesiredCapabilities dc;
        ChromeOptions chromeOptions = new ChromeOptions();
        chromeOptions.addArguments("--headless", "--windows-size=1920,1080");
        driver = new ChromeDriver(chromeOptions);
        return driver;
    }
    //启动火狐浏览器
    public WebDriver setupFirfox() throws IOException {
        String driverPath = new File("./").getCanonicalPath() + "/src/test/java/driver/geckodriver.exe";
        String driverName = "webdriver.gecko.driver";
        System.setProperty(driverName, driverPath);
        // System.setProperty("webdriver.firefox.bin", "C:/Program Files(x86)/Mozilla Firefox/firefox.exe");
        driver = new FirefoxDriver();
        //driver.get(test_url);
        driver.manage().window().maximize();
        // driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        return driver;
    }
    //启动Chrome-headless
    public WebDriver setupFirefoxHeadless() throws IOException {
        String driverPath = new File("./").getCanonicalPath() + "/src/test/java/driver/geckodriver.exe";
        String driverName = "webdriver.gecko.driver";
        System.setProperty(driverName, driverPath);
        FirefoxOptions firefoxOptions = new FirefoxOptions();
        firefoxOptions.addArguments("--headless", "--windows-size=1920,1080");
        driver = new FirefoxDriver(firefoxOptions);
        return driver;
    }
    //启动opera浏览器
    public WebDriver setupOpera() throws IOException {
        String driverPath = new File("./").getCanonicalPath() + "/src/test/java/driver/operadriver.exe";
        String driverName = "webdriver.opera.driver";
        System.setProperty(driverName, driverPath);
        // System.setProperty("webdriver.firefox.bin", "C:/Program Files(x86)/Mozilla Firefox/firefox.exe");
        driver = new OperaDriver();
        //driver.get(test_url);
        driver.manage().window().maximize();
        //driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        return driver;
    }
    //启动IE浏览器
  /*  public WebDriver setupIE(String test_url) {
        System.setProperty("webdriver.ie.driver", "C:\\Program Files\\Internet Explorer\\IEDriverServer.exe");
        DesiredCapabilities dc = DesiredCapabilities.internetExplorer();
        dc.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
        driver = new InternetExplorerDriver(dc);
        driver.get(test_url);
        return driver;
    }
*/
    //关闭浏览器
    public void teardownBrowser() {
        driver.close();
    }
    /**
     * 是否存在某个节点
     *
     * @param by
     * @param wd
     * @return
     */
    public boolean isPresentEle(By by, WebDriver wd) {
        try {
            wd.findElement(by);
            return true;
        } catch (Exception e) {
            return false;
        }
    }
    /**
     * 获取当前时间，并转换为字符串
     * @return
     */
    private static String Date2String(){
        Format format = new SimpleDateFormat("yyyyMMddHHmmss");
        return format.format(new Date());
    }
    /**
     * 自动截屏
     * @param wd
     */
    public  void takeScreenshot(WebDriver wd){
        try {
            //获取截图file
            File srcFile= ((TakesScreenshot)wd).getScreenshotAs(OutputType.FILE);
            //新文件
            File targeFile = new File("pic/" + Date2String()+".png");
            //将图片移动到指定位置
            FileUtils.copyFile(srcFile, targeFile);
        }  catch (IOException e) {
            e.printStackTrace();
        }
    }
    /**
     * 获取断言失败的原因、信息和截屏
     * @param one
     * @param two
     * @param wd
     * @throws IOException
     * @throws InterruptedException
     */
    public  void assertEqu(String one,String two,WebDriver wd) throws InterruptedException, IOException{
        try{
            Thread.sleep(3000);
            takeScreenshot(wd);
            assertEquals(one, two);
        }catch(Exception e){
            System.out.println(e.getCause());
            System.out.println(e.getMessage());
        }
    }
}